<template>
  <upload-excel
    :before-upload="beforeUpload"
    :on-success="onSuccess"
  />
</template>

<script>
import { importEmployeeAPI } from '@/api/employees'
import { getImportData } from '@/utils/excelData'
export default {
  name: 'Import',
  methods: {
    beforeUpload(file) { // 回调前
      console.log('file', file)
      const isLt1M = file.size / 1024 / 1024 < 1

      if (isLt1M) {
        return true
      }

      this.$message({
        message: 'Please do not upload files larger than 1m in size.',
        type: 'warning'
      })
      return false
    },
    // excel解析完毕自动调用的回调函数
    async onSuccess({ header, results }) {
      // 打印header和results
      const newArr = getImportData(results)
      console.log(header, results)
      // 调用导入接口
      await importEmployeeAPI(newArr)
      this.$router.back()// 返回到点击前的路由
    }
  }
}
</script>
